﻿using System;
using System.Collections.Generic;

namespace banking_system
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("\t\t\t\t\t\t欢迎使用国信银行账户系统");
            Console.WriteLine("\t\t\t\t\t\t\t\t\t\t\t\t版本：BetaV1.0");
            Console.WriteLine("------------------------------------------------------------------------------------------------------------------------");
            Console.WriteLine("------------------------------------------------------------------------------------------------------------------------");
            List<UserInfo> list = new List<UserInfo>();
            Boolean fla = true;
            while(fla)
            {
                Console.WriteLine("\t\t\t\t\t\t请选择服务功能");
                Console.WriteLine("\t\t\t\t\t\t1.创建账户");
                Console.WriteLine("\t\t\t\t\t\t2.用户登录");
                Console.WriteLine("\t\t\t\t\t\t3.退出系统");
                Console.WriteLine("------------------------------------------------------------------------------------------------------------------------");
                foreach (UserInfo item in list)
                {
                    Console.WriteLine("--" + item.userName + "--" + item.passWord+ "--");

                }
                string input = Console.ReadLine();
                if(!RegexValidator.IsValidNumber(input))
                {
                    Console.WriteLine("输入有误，请输入正确的选项序号!");
                    continue;
                }
                int num;
                //string转int
                int.TryParse(input, out num);
                decimal money = 200m;  //vip账户透支额度
                switch (num)
                {
                    case 1:
                        int count = 0;
                        Console.WriteLine("\t\t\t\t\t\t请选择创建账户类型");
                        Console.WriteLine("\t\t\t\t\t\t1.普通账户");
                        Console.WriteLine("\t\t\t\t\t\t2.vip账户");
                        string userName_type = Console.ReadLine();
                        if (!RegexValidator.IsValidNumber(userName_type))
                        {
                            Console.WriteLine("输入有误，请输入正确的选项序号!");
                            continue;
                        }
                        int type;
                        int.TryParse(userName_type, out type);
                        if(type == 1)  //创建普通账户
                        {
                            Console.Write("请输入账户名称：");
                            string userName = Console.ReadLine();
                            if (RegexValidator.IsChineseOnly(userName_type) || RegexValidator.IsEnglishOnly(userName_type))
                            {
                                Console.WriteLine("输入有误，名称只能为中文或英文!");
                                continue;
                            }
                            if (list.Count > 0)
                            {
                                foreach (UserInfo item in list)
                                {
                                    if ((item.userName).Equals(userName))
                                    {
                                        Console.WriteLine("该账户名字重复，请换一个！");
                                        break;
                                    } else
                                    {
                                        Console.Write("请输入账户密码：");
                                        string userPassword = Console.ReadLine();
                                        Console.Write("请确认账户密码：");
                                        string userPassword_again = Console.ReadLine();


                                        if (userPassword.Equals(userPassword_again))
                                        {
                                            Console.WriteLine("账户创建成功，请登录使用");
                                            list.Add(new UserInfo(1, userName, userPassword, 1, money));
                                            Console.WriteLine();
                                            break;
                                        }
                                        else
                                        {

                                            count++;
                                            if (count >= 2)
                                            {
                                                return;
                                            }
                                            Console.WriteLine("两次输入密码不一致！" + count);
                                            break;
                                        }

                                    }
                                }
                            } else
                            {
                                Console.Write("请输入账户密码：");
                                string userPassword = Console.ReadLine();
                                Console.Write("请确认账户密码：");
                                string userPassword_again = Console.ReadLine();
                                if (userPassword.Equals(userPassword_again))
                                {
                                    Console.WriteLine("账户创建成功，请登录使用");
                                    list.Add(new UserInfo(1, userName, userPassword, 1, money));
                                    Console.WriteLine();
                                    break;
                                }
                                else
                                {
                                    count++;
                                    if (count >= 2)
                                    {
                                        return;
                                    }
                                    Console.WriteLine("两次输入密码不一致！" + count);
                                    break;
                                }
                            }
                        } else if(type == 2)
                        {
                            // 创建vip账户
                            Console.Write("请输入账户名称：");
                            string userName = Console.ReadLine();
                            if (RegexValidator.IsChineseOnly(userName) || RegexValidator.IsEnglishOnly(userName))
                            {
                                Console.WriteLine("输入有误，名称只能为中文或英文!");
                                continue;
                            }
                            if (list.Count > 0)
                            {
                                foreach (UserInfo item in list)
                                {
                                    if ((item.userName).Equals(userName))
                                    {
                                        Console.WriteLine("该账户名字重复，请换一个！");
                                        break;
                                    } else
                                    {
                                        Console.Write("请输入账户密码：");
                                        string userPassword = Console.ReadLine();
                                        Console.Write("请确认账户密码：");
                                        string userPassword_again = Console.ReadLine();
                                        if (userPassword.Equals(userPassword_again))
                                        {
                                            Console.WriteLine("账户创建成功，请登录使用");
                                            list.Add(new UserInfo(1, userName, userPassword, 2, money));
                                            Console.WriteLine();
                                            break;
                                        }
                                        else
                                        {
                                            count++;
                                            if (count >= 2)
                                            {
                                                return;
                                            }
                                            Console.WriteLine("两次输入密码不一致！" + count);
                                        }
                                    }
                                }
                            } else
                            {
                                Console.Write("请输入账户密码：");
                                string userPassword = Console.ReadLine();
                                Console.Write("请确认账户密码：");
                                string userPassword_again = Console.ReadLine();
                                if (userPassword.Equals(userPassword_again))
                                {
                                    Console.WriteLine("账户创建成功，请登录使用");
                                    list.Add(new UserInfo(1, userName, userPassword, 2, money));
                                    Console.WriteLine();
                                    break;
                                }
                                else
                                {
                                    count++;
                                    if (count >= 2)
                                    {
                                        return;
                                    }
                                    Console.WriteLine("两次输入密码不一致！" + count);
                                }
                            }
                        }
                        break;

                    case 2:
                        int count2 = 0;
                        Console.Write("请输入账户名称：");
                        string userName_login = Console.ReadLine();
                        //Console.WriteLine("RegexValidator.IsChineseOnly(userName_login)" + RegexValidator.IsChineseOnly(userName_login));
                        Console.Write("请输入账户密码：");
                        string userPassword_login = Console.ReadLine();
                        if (list.Count > 0)
                        {
                           
                            foreach (UserInfo item in list)
                            {
                                if (item.userName.Equals(userName_login) && item.passWord.Equals(userPassword_login))
                                {

                                    Console.WriteLine("登录成功,您的账户是：" + (item.type == 1 ? "普通账户" : "vip账户"));
                                    Boolean fl = true;
                                    while (fl)
                                    {
                                        switch (gongNeng())
                                        {
                                            case 1:
                                                Console.WriteLine("请输入取款金额：");
                                                decimal mon = decimal.Parse(Console.ReadLine());
                                                if (!RegexValidator.IsValidNumber(userName_login))
                                                {
                                                    Console.WriteLine("输入有误，取款金额只能为大于0的正整数!");
                                                    continue;
                                                }
                                                if (item.type == 1 && mon > item.money)
                                                {
                                                    Console.WriteLine("账户余额不足");
                                                }
                                                else
                                                {
                                                    if (item.type == 2)
                                                    {
                                                        //vip账户透支余额
                                                        Console.WriteLine("vip账户透支余额");
                                                        if (item.money - mon > 0)
                                                        {
                                                            item.money = item.money - mon;
                                                            Console.WriteLine("取款成功！当前账户为" + (item.type == 1 ? "普通账户，" : "vip账户，") + "取款金额是：{0},当前账户余额为：{1}", mon, item.money);
                                                            gongNeng();
                                                        }
                                                        else
                                                        {
                                                            //启用透支额度
                                                            item.money = ((int)UserPermissions.Money + item.money) - mon;
                                                            Console.WriteLine("取款成功！当前账户为" + (item.type == 1 ? "普通账户，" : "vip账户，") + "取款金额是：{0},当前账户余额为：{1},当前透支额度为：{2}", mon, item.money, (int)UserPermissions.Money - item.money);
                                                            gongNeng();
                                                        }

                                                    }
                                                    else
                                                    {
                                                        item.money = item.money - mon;
                                                        Console.WriteLine("取款成功！当前账户为" + (item.type == 1 ? "普通账户，" : "vip账户，") + "取款金额是：{0},当前账户余额为：{1}", mon, item.money);
                                                        gongNeng();
                                                    }

                                                }

                                                break;
                                            case 2:
                                                
                                                Console.WriteLine("请输入存款金额：");
                                                string lap = Console.ReadLine();
                                                decimal m = decimal.Parse(lap);
                                                if (!RegexValidator.IsMoney(lap))
                                                {
                                                    Console.WriteLine("输入有误，存款金额只能为大于0的正整数!");
                                                    continue;
                                                }
                                                if (m > 0)
                                                {
                                                    item.money = item.money + m;
                                                    Console.WriteLine("操作成功！当前账户余额为：{0}元", item.money);
                                                    gongNeng();
                                                }
                                                else
                                                {
                                                    Console.WriteLine("操作失败！");
                                                    gongNeng();
                                                }

                                                break;
                                            case 3:
                                                Console.WriteLine("查询成功！当前账户为" + (item.type == 1 ? "普通账户，" : "vip账户，") + "当前账户余额为：{0}", item.money);
                                                break;
                                            case 4:
                                                
                                                Console.Write("请输入转入账户：");
                                                string zhanghu = Console.ReadLine();
                                                if (RegexValidator.IsChineseOnly(zhanghu) || RegexValidator.IsEnglishOnly(zhanghu))
                                                {
                                                    Console.WriteLine("输入有误，转入账户名称只能为中文或英文!");
                                                    continue;
                                                }
                                                Console.Write("请输入转入金额：");
                                                string p = Console.ReadLine();
                                                decimal zhangHu_jinE = decimal.Parse(p);
                                                if (!RegexValidator.IsMoney(p))
                                                {
                                                    Console.WriteLine("输入有误，转入金额金额只能为大于0的正整数!");
                                                    continue;
                                                }
                                                foreach (UserInfo it in list)
                                                {
                                                    if (it.userName.Equals(zhanghu) && it.money > zhangHu_jinE)
                                                    {
                                                        it.money = it.money + zhangHu_jinE;
                                                        Console.WriteLine("转账成功");
                                                        break;
                                                    } else
                                                    {
                                                        Console.WriteLine("转账失败");
                                                        break;
                                                    }
                                                }
                                                    break;
                                            case 5:
                                                Console.Write("请输入当前账户密码：");
                                                string miMa = Console.ReadLine();
                                                int num1 = 3;
                                                if (miMa.Equals(item.passWord))
                                                {
                                                    while(num >= 0)
                                                    {
                                                        Console.Write("请输入新密码密码：");
                                                        string miMa_new = Console.ReadLine();
                                                        Console.Write("请再次确认密码：");
                                                        string miMa_new2 = Console.ReadLine();
                                                        if (miMa_new.Equals(miMa_new2))
                                                        {
                                                            Console.WriteLine("修改成功");
                                                            break;
                                                        }
                                                        else
                                                        {
                                                            Console.WriteLine("两次输入密码不一致！" + num1);
                                                            num1--;
                                                        }
                                                    }
                                                    
                                                } else
                                                {
                                                    Console.WriteLine("密码修改失败！");
                                                }
                                                    break;
                                            case 6:
                                                
                                                fl = false;
                                                break;
                                        }
                                    }
                                    
                                }
                                else
                                {
                                    Console.WriteLine("登录失败，请检查账户和密码");
                                    count2++;
                                    if (count2 >= 2)
                                    {
                                        Console.WriteLine("退出系统,感谢您的使用，再见！");
                                        return;
                                    }
                                    continue;
                                }
                            }
                        }
                            break;
                    case 3:
                        Console.WriteLine("退出系统,感谢您的使用，再见！");
                        return;
                    default:
                        continue;
                }
            }

        }
    
    public static int gongNeng()
        {
            Console.WriteLine("------------------------------------------------------------------------------------------------------------------------");
            Console.WriteLine("\t\t\t\t\t\t请选择服务功能");
            Console.WriteLine("\t\t\t\t\t\t1.取款服务");
            Console.WriteLine("\t\t\t\t\t\t2.存款服务");
            Console.WriteLine("\t\t\t\t\t\t3.查询余额");
            Console.WriteLine("\t\t\t\t\t\t4.转账服务");
            Console.WriteLine("\t\t\t\t\t\t5.修改密码");
            Console.WriteLine("\t\t\t\t\t\t6.返回上级菜单");
            Console.WriteLine("------------------------------------------------------------------------------------------------------------------------");
            string service = Console.ReadLine();
            int ser;
            int.TryParse(service, out ser);
            return ser;
        }
    }
}
